***************************************************************************************************************
* Name: TableA2
* Description: Content of Table is generated with final dataset created by running this file. It also generates dataset used as input for Figure 1
* Reference: "Competition and Civilian Victimization"
***************************************************************************************************************

use "violence_t_export.dta",clear
keep if victim_farc~=.&victim_paras~=.

*****Log likelihood definitions*****
set more off

tab action_paras2, gen(prP)
global yP1 "prP1"
global yP2 "prP2"
global yP3 "prP3"

tab action_farc2, gen(prF)
global yF1 "prF1"
global yF2 "prF2"
global yF3 "prF3"

*Types unrestricted

capture program drop lfmsec_ur
program lfmsec_ur
  version 13.1
  *numbers denote actions and letters denote players
  *this model allows heterogeneity across actions but assummes same effect of control variables across players
  args lnf xb2P xb3P xb2F xb3F 
  tempvar p1F p2F p3F p1P p2P p3P 
  quietly {
     
	 gen double `p1F' = 1/(1+exp(`xb2F')+exp(`xb3F'))
     gen double `p2F' = exp(`xb2F')/(1+exp(`xb2F')+exp(`xb3F'))
     gen double `p3F' = exp(`xb3F')/(1+exp(`xb2F')+exp(`xb3F'))
     
	 gen double `p1P' = 1/(1+exp(`xb2P')+exp(`xb3P'))
     gen double `p2P' = exp(`xb2P')/(1+exp(`xb2P')+exp(`xb3P'))
     gen double `p3P' = exp(`xb3P')/(1+exp(`xb2P')+exp(`xb3P'))
     
	 replace `lnf' = $yP1*ln(`p1P') + $yP2*ln(`p2P') + $yP3*ln(`p3P') + $yF1*ln(`p1F') + $yF2*ln(`p2F') + $yF3*ln(`p3F')
   }
end

capture program drop game2stage_types
 program game2stage_types, eclass
	syntax [if]
	marksample touse
	tempname b b1 res_2F_2 res_3F_2 res_2F_3 res_3F_3 res_2P_2 res_3P_2 res_2P_3 res_3P_3
	
		foreach var	 in  nbi_t lpobl_tot_t share_left gini_i army_dist dmr evlp paras_dist farc_dist{
			mkspline `var's = `var', cubic
		}
		
		mkspline royal1 .0000623 royal2 = royalties_t
		mkspline coca1 0.12 coca2 = coca_t

		capture noisily {
		quietly{
			mlogit action_paras2 nbi_ts1-evlps4 paras_dists1-paras_dists4 royal1-coca2 i.time#c.(army_dists1-evlps4) gcaribe gpacifica gorinoquia gamazonia if victim_farc~=., cluster(muni_code) 
			predict predP2,outcome(2)
			predict predP3,outcome(3)
			}
			display "first stage AUC " _rc
		}
		if _rc~=0{
			drop predP2 predP3 nbi_ts1-coca2
			matrix `b'=J(1,176,0)
		}
		else{
			set maxiter 500
			capture noisily {
				quietly{
					mlogit action_farc2 nbi_ts1-evlps4 farc_dists1-farc_dists4 royal1-coca2 i.time#c.(army_dists1-evlps4) gcaribe gpacifica gorinoquia gamazonia if victim_paras~=., cluster(muni_code)
					}
			}  
			display "first stage FARC " _rc		
			if _rc~=0 {
				drop predP2 predP3 nbi_ts1-coca2
				matrix `b'=J(1,176,0)
				}
			else{
				predict predF2,outcome(2)
				predict predF3,outcome(3)
			
				ml model lf lfmsec_ur (eq1: nbi_t royalties_t coca_t share_left lpobl_tot_t army_dist ///
				dmr gini_i time gcaribe gpacifica gorinoquia gamazonia ///
				evlp paras_dist predF2 predF3) /// 
				(eq2: nbi_t royalties_t coca_t share_left lpobl_tot_t army_dist ///
				dmr gini_i time gcaribe gpacifica gorinoquia gamazonia ///
				evlp paras_dist predF2 predF3) /// 
				(eq3: nbi_t royalties_t coca_t share_left lpobl_tot_t army_dist ///
				 dmr gini_i time gcaribe gpacifica gorinoquia gamazonia ///
				evlp farc_dist predP2 predP3) ///
				(eq4: nbi_t royalties_t coca_t share_left lpobl_tot_t army_dist ///
				 dmr gini_i time gcaribe gpacifica gorinoquia gamazonia ///
				evlp farc_dist predP2 predP3),
				capture noisily ml maximize, diff
				display "second stage" _rc
				if _rc~=0{
							matrix `b1'=J(1,72,0)
							matrix `res_2F_2'=J(1,13,0)
							matrix `res_3F_2'=J(1,13,0)
							matrix `res_2F_3'=J(1,13,0)
							matrix `res_3F_3'=J(1,13,0)
							matrix `res_2P_2'=J(1,13,0) 
							matrix `res_3P_2'=J(1,13,0) 
							matrix `res_2P_3'=J(1,13,0) 
							matrix `res_3P_3'=J(1,13,0)
						}
				else{
						matrix `b1'=e(b)
						quietly{
							*Predicted responses of farc 
							margins , at(predP2=(0.2(0.05)0.8) time=2 predP3=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq3)))/(1+exp(predict(equation(eq3)))+exp(predict(equation(eq4))))) nose
							matrix `res_2F_2'=r(b)
							margins , at(predP2=(0.2(0.05)0.8) time=2 predP3=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq4)))/(1+exp(predict(equation(eq3)))+exp(predict(equation(eq4))))) nose
							matrix `res_3F_2'=r(b)

							margins , at(predP3=(0.2(0.05)0.8) time=2 predP2=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq3)))/(1+exp(predict(equation(eq3)))+exp(predict(equation(eq4))))) nose
							matrix `res_2F_3'=r(b)
							margins , at(predP3=(0.2(0.05)0.8) time=2 predP2=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq4)))/(1+exp(predict(equation(eq3)))+exp(predict(equation(eq4))))) nose
							matrix `res_3F_3'=r(b)

							*Predicted responses of paras 
							margins , at(predF2=(0.2(0.05)0.8) time=2 predF3=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq1)))/(1+exp(predict(equation(eq1)))+exp(predict(equation(eq2))))) nose
							matrix `res_2P_2'=r(b)
							margins , at(predF2=(0.2(0.05)0.8) time=2 predF3=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq2)))/(1+exp(predict(equation(eq1)))+exp(predict(equation(eq2))))) nose
							matrix `res_3P_2'=r(b)

							margins , at(predF3=(0.2(0.05)0.8) time=2 predF2=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq1)))/(1+exp(predict(equation(eq1)))+exp(predict(equation(eq2))))) nose
							matrix `res_2P_3'=r(b)
							margins , at(predF3=(0.2(0.05)0.8) time=2 predF2=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq2)))/(1+exp(predict(equation(eq1)))+exp(predict(equation(eq2))))) nose
							matrix `res_3P_3'=r(b)
							}
					}
			matrix `b'=(`b1',`res_2F_2',`res_3F_2',`res_2F_3',`res_3F_3',`res_2P_2',`res_3P_2',`res_2P_3',`res_3P_3')
			drop predF2 predF3 predP2 predP3 nbi_ts1-coca2	
			}


		}	
	ereturn post `b' ,esample(`touse')		
	
end

game2stage_types
matrix b_m=e(b)
matrix res_boot_m=b_m

set seed 1313

set matsize 800

forvalues i = 1/500{
	preserve
	*bsample ,cluster(muni_code)
	bsample
	game2stage_types
	matrix b_m=e(b)
	matrix res_boot_m=(res_boot_m\b_m)
	display "Iteration: " `i'
	restore
}

svmat double res_boot_m
keep res_boot_m1-res_boot_m176


*This dataset is used by best-response-graphs-type.R
save "boot_res_region_t.dta",replace
use "boot_res_region_t.dta",clear

forvalue i=1/176 {
	replace res_boot_m`i'=. if res_boot_m`i'==0
}

tabstat res_boot_m1-res_boot_m176 if _n~=1,s(sd) save
matrix sd_mat=r(StatTotal)
keep if _n==1
gen dum=1
reshape long res_boot_m, i(dum) j(estimates)
drop dum estimates
rename res_boot estimates
matrix sd_mat=sd_mat'
svmat sd_mat

*Generating pvals
gen pval=min(2*normal(estimates/sd_mat),2*(1-normal(estimates/sd_mat)))

drop if _n>=73

list sd_mat
list pval
